package com.ltsznh.android.locationreporting;

import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.ltsznh.android.locationreporting.sync.SyncThread;


public class SettingActivity extends Activity {
    private SyncThread thread;

    private AccountManager accountManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        ActionBar actionBar = this.getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    public void sync(View view) {
        if (thread == null) {
            thread = new SyncThread(SettingActivity.this);
            thread.start();
        } else {
            if (thread.isAlive()) {
                try {
                    thread.stopSync();
                    thread.join(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                thread = null;
                thread = new SyncThread(SettingActivity.this);
                thread.start();
            }
        }
    }


    public void login(View view) {
//        Intent intent = new Intent();
//        intent.setClass(this, LoginActivity.class);
//        startActivity(intent);


        accountManager = AccountManager.get(this);

        addNewAccount("com.ltsznh", "Full access");
    }

    /**
     * Add new account to the account manager
     *
     * @param accountType
     * @param authTokenType
     */
    private void addNewAccount(String accountType, String authTokenType) {
        final AccountManagerFuture<Bundle> future = accountManager.addAccount(accountType, authTokenType, null, null, this, new AccountManagerCallback<Bundle>() {
            @Override
            public void run(AccountManagerFuture<Bundle> future) {
                try {
                    Bundle bnd = future.getResult();
                    showMessage("Account was created");
                    Log.d("udinic", "AddNewAccount Bundle is " + bnd);

                } catch (Exception e) {
                    e.printStackTrace();
                    showMessage(e.getMessage());
                }
            }
        }, null);
    }

    private void showMessage(final String msg) {
        if (TextUtils.isEmpty(msg))
            return;

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                Log.d(this.getClass().toString(), "click nothing");
                return super.onOptionsItemSelected(item);
        }
    }
}
